package cn.com.huzo.service.purchase.impl;

import cn.com.huzo.model.entities.purchase.PoLinesMrpLinesEntity;
import huzofw.model.entities.paginator.Paginator;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.collections4.MapUtils;
import cn.com.huzo.service.purchase.PoLinesMrpLinesManager;
import cn.com.huzo.repository.purchase.PoLinesMrpLinesRepository;

/**
 * 引入的调达表管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("PoLinesMrpLinesManager")
public class PoLinesMrpLinesManagerImpl extends GenericManagerImpl<PoLinesMrpLinesEntity, String>
        implements PoLinesMrpLinesManager {

    @Resource(name = "PoLinesMrpLinesRepository")
    private PoLinesMrpLinesRepository _dao_lines_mrp_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao_lines_mrp_lines.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<PoLinesMrpLinesEntity> itemList = _dao_lines_mrp_lines.fetchForPage(criterion);
            Paginator<PoLinesMrpLinesEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }
}
